package com.wzf.springdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzf.springdemo.bean.BusDevice;
import com.wzf.springdemo.constant.Constants;
import com.wzf.springdemo.mapper.BusDeviceMapper;
import com.wzf.springdemo.service.BusDeviceService;
import com.wzf.springdemo.utils.FastJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BusDeviceImpl extends ServiceImpl<BusDeviceMapper, BusDevice> implements BusDeviceService {
    @Autowired
    private BusDeviceMapper bdm;

    @Autowired
    private FastJsonUtil fjUtil;

    @Override
    public JSONObject getOnly(String data) {
        // 构建ws接口返回值
        JSONObject resMain = new JSONObject();
        JSONObject resPart1 = new JSONObject();
        JSONArray resPart2 = new JSONArray();

        // 解析输入参数
        JSONObject obj = JSON.parseObject(data);

        // 验证访问密钥
        // 构建db参数
        Map<String, Object> paramMap = new HashMap<String, Object>();

        // 调用db接口
        List<Map<String, Object>> dataResults = bdm.getOnly(paramMap);
        // 获取db接口返回值
        if(Constants.PROC_RETURN_OK == Integer.parseInt(paramMap.get("vErrVal").toString())){
            // 结果集处理
            resPart2 = fjUtil.getJSONArray(dataResults);
            // 存储过程正常处理
            resPart1.put(Constants.RESULT_JSON_PART1_RESULT, Constants.PROC_RETURN_OK);
        }else{
            // 存储过程异常处理
            resPart1.put(Constants.RESULT_JSON_PART1_RESULT, paramMap.get("vErrVal"));
            resPart1.put(Constants.RESULT_JSON_PART1_ERRMSG, paramMap.get("vErrMsg"));
        }

        // 合并返回值
        resMain.put(Constants.RESULT_JSON_PART1_NAME, resPart1);
        resMain.put(Constants.RESULT_JSON_PART2_NAME, resPart2);

        return resMain;



    }
}
